﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.Templet;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormUpdatePassword : FormTemplet
    {
        private Point mousePoint = default(Point);

        public FormUpdatePassword()
        {
            this.InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (this.txtPassword.Text == "" || this.txtPassword.Text.Length > 32)
            {
                MessageBox.Show("密码不能为空 或者 不能大于32个字符", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtPassword.Focus();
            }
            else if (this.txtNewPassword.Text == "" || this.txtNewPassword.Text.Length > 32)
            {
                MessageBox.Show("新密码不能为空 或者 不能大于32个字符", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtNewPassword.Focus();
            }
            else if (this.txtNewPassword.Text != this.txtNewPassword2.Text)
            {
                MessageBox.Show("两次输入的新密码不一致", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtNewPassword2.Focus();
            }
            else
            {
                Operators operators = new Operators();
                DataTable dataTable = null;
                try
                {
                    dataTable = operators.LoadUser(this.txtLoginName.Text.Trim(), this.txtPassword.Text);
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                operators.Dispose();
                if (dataTable != null)
                {
                    if (dataTable.Rows.Count > 0)
                    {
                        if (this.txtPassword.Text == dataTable.Rows[0][3].ToString())
                        {
                            operators = new Operators();
                            bool flag = false;
                            try
                            {
                                flag = operators.UpdatePassword(this.txtLoginName.Text.Trim(), this.txtNewPassword.Text);
                            }
                            catch (SqlException ex)
                            {
                                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                            }
                            catch (Exception ex2)
                            {
                                MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                            operators.Dispose();
                            if (flag)
                            {
                                MessageBox.Show("更改密码成功", "Successful", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                                LoginInfo.Password = this.txtNewPassword.Text;
                                dataTable.Dispose();
                                base.Close();
                            }
                        }
                        else
                        {
                            MessageBox.Show("您输入的账号或者密码错误", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                    else
                    {
                        MessageBox.Show("您输入的账号或者密码错误", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.txtPassword.Focus();
                        this.txtPassword.SelectAll();
                    }
                    dataTable.Dispose();
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void FormUpdatePassword_Load(object sender, EventArgs e)
        {
            this.txtLoginName.Text = LoginInfo.LoginName;
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                base.Left = Control.MousePosition.X - this.mousePoint.X;
                base.Top = Control.MousePosition.Y - this.mousePoint.Y;
            }
        }

        private void Form_MouseUp(object sender, MouseEventArgs e)
        {
            this.Cursor = Cursors.Default;
        }

        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.mousePoint.X = e.X;
                this.mousePoint.Y = e.Y;
                this.Cursor = Cursors.Hand;
            }
        }
    }
}
